package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import stuff.DialogDisposeAction;

public class CustomJDialog  extends CustomJPanel{
	
	private static final long serialVersionUID = 1L;
	protected JFrame parent;
	protected JPanel titlePanel, contentPanel;
	protected JLabel titlebar;
	protected JButton exitButton;
	private int cornerIndex;
	
	public CustomJDialog(JFrame param, JFrame parent, int cornerIndex) {
		super(param, cornerIndex);
		this.parent = parent;
		this.cornerIndex = cornerIndex;
		setLayout(new BorderLayout());
		titlePanel = new JPanel(new BorderLayout());
		titlePanel.setOpaque(false);
		titlebar = new JLabel("ThiGPLX Server");
		titlebar.setForeground(new Color(255, 255, 255, 200));
		titlePanel.setBorder(new EmptyBorder(0, 10, 0, 3));
		JPanel topRight = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		exitButton = new CustomJButton("x", 20, 20, 
				CustomJButton.RED, CustomJButton.LIGHTER, Color.DARK_GRAY, 0, true, false);
		exitButton.setFont(exitButton.getFont().deriveFont(10f));
		exitButton.setAction(new DialogDisposeAction(param, parent));
		topRight.add(exitButton);
		topRight.setOpaque(false);
		titlePanel.add(titlebar,BorderLayout.CENTER);
		titlePanel.add(topRight,BorderLayout.EAST);
		
		contentPanel = new JPanel(new BorderLayout());
		contentPanel.setOpaque(false);
		
		add(titlePanel,BorderLayout.NORTH);
		add(contentPanel,BorderLayout.CENTER);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = null;
		if(isResizing){
			g.setColor(new Color(60, 60, 60));
	        g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), cornerIndex, cornerIndex);
	        g.setColor(new Color(240, 240, 240));
			g.clearRect(0, 0+30, this.getWidth(), this.getHeight()-30);
	        g.fillRect(0, 0+30, this.getWidth(), this.getHeight()-30);
	        g.setColor(new Color(60, 60, 60));
	        g.drawRect(0, 0+30, this.getWidth(), this.getHeight()-30);
		}else{
			 g2d = (Graphics2D) g;
			g2d.setColor(new Color(60, 60, 60));
			g2d.setRenderingHint(
				    RenderingHints.KEY_ANTIALIASING,
				    RenderingHints.VALUE_ANTIALIAS_ON);
			g2d.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), cornerIndex, cornerIndex);
			
			g2d.setColor(new Color(240, 240, 240));
	        g2d.clearRect(0, 0+30, this.getWidth(), this.getHeight()-30);
	        g2d.fillRect(0, 0+30, this.getWidth(), this.getHeight()-30);
	        
	        g2d.setColor(new Color(100, 100, 100));
	        g2d.drawRect(0, 0+30, this.getWidth()-1, this.getHeight()-30-1);
	        
	        g2d.setRenderingHint(
				    RenderingHints.KEY_ANTIALIASING,
				    RenderingHints.VALUE_ANTIALIAS_DEFAULT);
		}
		
		
	}

	public void setExitAction(Action a){
		exitButton.setAction(a);
	}

	public void setTitle(String param){
		this.titlebar.setText(param);
	}
	
	public void setContentLayout(JPanel panel){
		contentPanel.removeAll();
		contentPanel.add(panel, BorderLayout.CENTER);
	}
	
}
